package mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils.ActivityUtils;
import mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF.BaseFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.activity.NewsDetailActivity;


/**
 * Created by ${cbf} on 2017/11/24.
 * 学习爬取网络数据进行分析并获取自己想要的数据
 */

public class NewsSportFragment extends BaseFragment {

    private ListView recyclerView;

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_shop_mall;
    }

    @Override
    protected void findViews() {
        recyclerView = (ListView) baseView.findViewById(R.id.recyclerview);

    }

    @Override
    protected void init() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                Document doc = null;//doc.string()为该url的文本字符串
                try {
                    doc = Jsoup.parse(new URL("http://lol.qq.com/web201310/info-heros.shtml"), 300);
                    Document document = Jsoup.parse(doc.toString());
                    Elements element = document.select("#seleteChecklist");    //取得标题所在<ul>的id值,通过
                    Document document1 = Jsoup.parse(element.toString());
                    Elements elements = document1.getElementsByTag("li");
                    if (elements == null) {
                        Log.v("cbf", "elements为空");
                    }
                    for (Element links : elements) {
                        String title = links.getElementsByTag("label").text();
                        Log.v("cbf", "title=" + title);
//                        tvShopMallDes.setText(title);
                    }

                    Document docs = Jsoup.parse(new URL("https://voice.hupu.com/nba/"), 300);
                    Document documents = Jsoup.parse(docs.toString());
                    Elements titleLinks = documents.select("div.list-hd");
                    List<String> list = new ArrayList<String>();
                    final List<String> urlList = new ArrayList<String>();
                    for (Element e : titleLinks) {
                        String t = e.select("a").text();
                        list.add(e.select("a").text());
                        String uri = e.select("a").attr("href");
                        Log.v("cbf", "t=" + t + " uri=" + uri);
                        urlList.add(e.select("a").attr("href"));
                    }

                    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.test_list_item, list);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            recyclerView.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }
                    });
                    recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                            Bundle bundle = new Bundle();
                            bundle.putString("url", urlList.get(position));
                            ActivityUtils.skipActivity(NewsDetailActivity.class, bundle);
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void setListeners() {

    }

    @Override
    public void onClick(View view) {

    }

    public static Fragment Instance(Context context, String mallInfo) {
        NewsSportFragment homeFragment = new NewsSportFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", mallInfo);
        homeFragment.setArguments(bundle);
        return homeFragment;
    }
}
